package com.tulele.vtool.task.async;

import com.alibaba.fastjson.JSONObject;
import com.tulele.vtool.bus.model.UserInfo;
import com.tulele.vtool.bus.model.thirdparty.WxPushUserInfo;
import com.tulele.vtool.bus.service.ILookArticleService;
import com.tulele.vtool.bus.service.IUserService;
import com.tulele.vtool.bus.service.SysGzhService;
import com.tulele.vtool.model.PlatformSysInfo;
import com.tulele.vtool.util.RedisCacheUtil;
import com.tulele.vtool.util.SysConstant;
import com.tulele.vtool.util.ToolUtil;
import com.tulele.vtool.wechat.service.WeixinService;
import com.tulele.vtool.wechat.service.WeixinServiceDL;
import com.tulele.vtool.wechat.service.WeixinServiceMap;
import lombok.extern.slf4j.Slf4j;
import me.chanjar.weixin.common.error.WxErrorException;
import me.chanjar.weixin.mp.bean.kefu.WxMpKefuMessage;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.ObjectFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Component;

import java.util.Optional;

@Component
@Slf4j
public class ThirdTimeRemindUserActivePushAsyncTask {

    @Autowired
    private RedisCacheUtil redisCacheUtil;

    @Autowired
    private ObjectFactory<WeixinServiceDL> objectFactory;

    @Autowired
    private SysGzhService sysGzhService;

    @Autowired
    private ILookArticleService lookArticleService;

    @Autowired
    private IUserService userService;

//    @Value("${frontDomain}")
//    private String frontDomain;

    //20200512 调整参数，多线程跑
    @Async("taskExecutor")
    public void execute(String redisKey, String profile, UserInfo userInfo) throws Exception {

//        log.info("[enter ThirdTimeRemindUserActivePushAsyncTask] = " + redisKey);

        Object pushUserJsonObject = redisCacheUtil.getCacheObject(redisKey);
        if (pushUserJsonObject != null) {

            String pushUserJson = (String)pushUserJsonObject;
            log.info("[pushUserJson] = " + pushUserJson);

            WxPushUserInfo wxPushUserInfo = JSONObject.parseObject(pushUserJson, WxPushUserInfo.class);

            int lookMeUserCount = Optional.ofNullable(lookArticleService.getLookArticleUserCount(wxPushUserInfo.getUserId())).orElse(0);

            if (lookMeUserCount > 1) {

                String customerPageClickInfo = (String)redisCacheUtil.getCacheObject("customerPageWxPushUser" + wxPushUserInfo.getUserId());

                if (StringUtils.isNotBlank(customerPageClickInfo)) {

                    int vipStatus = Optional.ofNullable(userService.getUserVipStatus(wxPushUserInfo.getUserId())).orElse(0);

                    if (vipStatus == 0) {
                        PlatformSysInfo sysGzh = sysGzhService.getPlatformSysInfo(profile);
                        String kefuMsg = SysConstant.THIRD_TIME_REMIND_USER_ACTIVE_MSG;
                        kefuMsg=kefuMsg.replace(SysConstant.frontDomain,sysGzh.getFrontDomain());//替换域名 20200218
                        if(userInfo.getPhoneModel().equals(SysConstant.IOS)){
                            kefuMsg = ToolUtil.handleMsgmenuidByIOS(kefuMsg);
                        }
                        WxMpKefuMessage message=WxMpKefuMessage.TEXT().toUser(wxPushUserInfo.getOpenId()).content(kefuMsg).build();
                        try {
                            WeixinServiceDL weixinService= WeixinServiceMap.getWeixinService(profile);
                            if(weixinService==null) {
                                weixinService = objectFactory.getObject();
                                weixinService.reSetConfigStorage(profile);
                                WeixinServiceMap.putWeixinService(profile,weixinService);
                            }
                            weixinService.getKefuService().sendKefuMessage(message);
                        } catch (WxErrorException e) {
                            e.printStackTrace();
                        }
                    }
                }
            }
        }
    }
}
